﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Apollo.Core
{
    public class FormItem
    {
        public FormItem() {
            this.RowSpan = 1;
            this.ColSpan = 1;
        }
        
        public Projection Projection { get; set; }

        public FormItemType Type { get; set; }

        public string DefaultValue { get; set; }

        public string CustomFormItemControl { get; set; }

        /// <summary>
        /// Column identifier; zero based
        /// </summary>
        public int Col { get; set; }

        /// <summary>
        /// Row identifier; zero based
        /// </summary>
        public int Row { get; set; }

        public int ColSpan { get; set; }

        public int RowSpan { get; set; }

        public FormGroup Group { get; set; }

        public XElement ToXml() {
            XElement xelem = new XElement("property");
            xelem.Add(new XAttribute("col", this.Col.ToString()));
            xelem.Add(new XAttribute("row", this.Row.ToString()));
            xelem.Add(new XAttribute("type", this.Type.ToString()));

            if(this.ColSpan > 1)
                xelem.Add(new XAttribute("colspan", this.ColSpan.ToString()));

            if (this.RowSpan > 1)
                xelem.Add(new XAttribute("rowspan", this.ColSpan.ToString()));

            if(!string.IsNullOrEmpty(this.CustomFormItemControl))
                xelem.Add(new XAttribute("customcontrol", this.CustomFormItemControl));

            xelem.SetValue(this.Projection.ProjectionPath);

            return xelem;
        }

        public static FormItem FromXml(XElement xelement, FormGroup group) {
            FormItem item = new FormItem();

            item.Group = group;

            if (xelement.Name != "property")
                throw new ArgumentException("Invalid XML format! Expected name 'property', but got '" + xelement.Name + "'");

            if (xelement.Attribute("type") == null)
                throw new ArgumentException("Invalid XML format! Missing required attribute 'type'");

            item.Type = (FormItemType)System.Enum.Parse(typeof(FormItemType), xelement.Attribute("type").Value);
            item.Col = int.Parse(xelement.Attribute("col").Value);
            item.Row = int.Parse(xelement.Attribute("row").Value);

            if (xelement.Attribute("colspan") != null)
                item.ColSpan = int.Parse(xelement.Attribute("colspan").Value);
            else
                item.ColSpan = 1;

            if (xelement.Attribute("rowspan") != null)
                item.RowSpan = int.Parse(xelement.Attribute("rowspan").Value);
            else
                item.RowSpan = 1;

            if (xelement.Attribute("customcontrol") != null)
                item.CustomFormItemControl = xelement.Attribute("customcontrol").Value;

            item.Projection = new Projection(xelement.Value, item.Group.Form.Entity.MappedClass);

            return item;
        }

    }

    public enum FormItemType { 
        ReadOnly = 0,
        Normal = 1,
        Required = 2,
        Recommanded = 3
    }
}
